<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>

<script>
  // requestAnimationFrame 配合 canvas 实现动画
  // 目标:画一个方形，让这个方形随着时间向右移动
  // 原理:每隔一段小时间，让方形x变大就行

  //   substr(2, 6) 一样的
  const a = "#" + Math.random().toString(16).substring(2, 8); // 随机颜色生成
  console.log(a);

  const canvas = document.createElement("canvas");
  canvas.width = 600;
  canvas.height = 400;
  document.body.append(canvas);
  const context = canvas.getContext("2d");

  context.fillStyle = "#093227";
  let x = 0;

  function loop() {
    if (x >= 500) return;
    context.clearRect(x, 100, 100, 100);
    x++; // 1
    context.fillRect(x, 100, 100, 100);
    requestAnimationFrame(loop);
  }

  //   requestAnimationFrame(loop);
</script>
